package br.com.controller;

import java.util.List;

import br.com.model.Conto;
import br.com.model.Ponto;
import br.com.model.Usuario;
import br.com.model.dao.ConnectionManager;
import br.com.model.dao.DAO;
import br.com.model.dao.DAOFactory;

public class Executor
{
	static DAO infDao = DAOFactory.infDao( );
	
	public static void main( String[] args ) 
	{
		List<Conto> contos = infDao.busca( "select c from Conto c", Conto.class );

		for( Conto conto : contos )
		{
			List<Ponto> pontos = infDao.busca( "select p from Ponto p where p.conto.IdConto = " + conto.getIdConto( ), Ponto.class );
			
			long tot = 0l;
			
			for( Ponto ponto : pontos )
			{
				tot += ponto.getPontos( );
			}
			
			System.out.println( "Conto:              " + conto.getTitulo( )   );
			System.out.println( "Total de pontos:    " + tot                  );
			System.out.println( "Pontos por usuairo: " + tot / pontos.size( ) );
			System.out.println();
		}
		
		ConnectionManager.closeAll( infDao );
	}
	
	public static void teste( )
	{
		Usuario u = infDao.busca( 2, Usuario.class );
		Usuario u2 = infDao.busca( 1, Usuario.class );
		
		Conto c = new Conto( );
		c.setConto( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" );
		c.setTitulo( "Terceiro" );
		c.setUsuario( u );
		
		criarPonto( c, u,  0 );
		criarPonto( c, u2, 0 );
	}
	
	private static void criarPonto( Conto c, Usuario u, long pontos )
	{
		Ponto p = new Ponto( );
		p.setConto( c );
		p.setPontos( pontos );
		p.setUsuario( u );
		
		infDao.save( p );
	}
}